#!/usr/bin/perl -w

use strict;

my $size = shift;

open FILE, "<ZuluWordList.txt";
open OUT, ">${size}LetterWords.txt";

my %goodsegs;
while(my $line = <FILE>) {
    next if $line =~ /,/;
    $line =~ /^(\w+)/;
    $line = $1;
    for(my $i=0;$i<length($line)-($size-1);$i++) {
	my $seg = substr($line,$i,$size);
	if(!defined($goodsegs{$seg})) {
	    $goodsegs{$seg} = 1;
	}
	else {
	    $goodsegs{$seg}++;
	}
    }


    # $line =~ s/\w+\s+//;
    # next if $line =~ /,/;
    # chomp($line);
    # my @seg = split(/ /,$line);
    # foreach my $item(@seg) {
    # 	if(length($item) == $size) {
    # 	    if(!defined($goodsegs{$item})) {
    # 		$goodsegs{$item} = 1;
    # 	    }
    # 	    else {
    # 		$goodsegs{$item}++;
    # 	    }
    # 	}
    # }
}
my $segcount = 0;
my $usegcount = 0;
foreach my $key (keys %goodsegs) {
    print OUT "$key\n";
    print "$key => $goodsegs{$key}\n";
    $segcount += $goodsegs{$key};
    $usegcount++;
}

print "unique $size letter segments: $usegcount\n";
print "total $size letter segments: $segcount\n";
